<template>
  <!-- 确认中标 -->
  <el-dialog
    v-model="props.visible"
    width="800px"
    :show-close="false"
    :close-on-click-modal="false"
    :before-close="handleClose"
  >
    <template #header="{ close, titleId, titleClass }">
      <div class="my-header">
        <h4
          :id="titleId"
          :class="titleClass"
        >
          {{ t('userCenter.t7') }}
        </h4>
        <img
          src="@/assets/img/icon_guanbi.png"
          @click="close"
          class="close"
        />
      </div>
    </template>

    <el-form
      ref="formEl"
      :model="formData"
      :rules="rules"
      hide-required-asterisk
    >
      <div v-if="dictData['win_bid_condition']">
        <div
          class="flexsc nowrap"
          v-for="(row, index) in formData.confirmQuotaBoList"
        >
          <!-- <el-form-item
            :prop="`confirmQuotaBoList.${index}.acceptCondition`"
            :rules="rules.acceptCondition"
            class="mr-2"
            style="width: 200px"
          >
            <el-select
              v-model="row.acceptCondition"
              :placeholder="t('userCenter.p7')"
               filterable
            >
              <el-option
                v-for="item in dictData['win_bid_condition']"
                :key="item.dictValue"
                :label="item.dictLabel"
                :value="item.dictValue"
                :disabled="winBidOptionsDisabled(item)"
              />
            </el-select>
          </el-form-item> -->
          <el-form-item
            :prop="`confirmQuotaBoList.${index}.acceptConditionDescription`"
            :rules="rules.acceptConditionDescription"
            class="mr-2"
            style="width: 100%"
          >
            <el-input
              type="textarea"
              v-model="row.acceptConditionDescription"
              :placeholder="t('userCenter.p8')"
              :autosize="{ minRows: 6 }"
              maxlength="500"
            />
          </el-form-item>
          <!-- <div
            style="width: 90px"
            class="mb-2"
          >
            <el-button
              :disabled="formData.confirmQuotaBoList.length === 1"
              @click="formData.confirmQuotaBoList.splice(index, 1)"
            >
              -
            </el-button>
            <el-button
              v-if="index === formData.confirmQuotaBoList.length - 1"
              :disabled="formData.confirmQuotaBoList.length === dictData['win_bid_condition'].length"
              @click="formData.confirmQuotaBoList.push({})"
            >
              +
            </el-button>
          </div> -->
        </div>
      </div>
    </el-form>

    <template #footer>
      <div class="dialog-footer flexcc">
        <el-button
          color="#252628"
          plain
          @click="cancel"
        >
          {{ t('cancelText') }}
        </el-button>
        <el-button
          type="primary"
          color="#252628"
          @click="confirm"
        >
          {{ t('confirmText') }}
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup>
import { onBeforeMount, reactive, ref } from 'vue'
import { useI18n } from 'vue-i18n'
import useDict from '@/hooks/useDict'
import { confirmQuotationApi as dockOrderConfirmQuotationApi } from '@/service/dockOrder'
import { confirmQuotationApi2 as procurementConfirmQuotationApi } from '@/service/procurement'
import { confirmQuotationApi as externalAssistanceConfirmQuotationApi } from '@/service/externalAssistance'

const { dictData, getDictData } = useDict()
const { t, locale } = useI18n()

onBeforeMount(() => {
  init()
})

const props = defineProps({
  visible: {
    type: Boolean,
    default: false
  },
  // 业务id
  objId: {
    type: [String, Number],
    required: true
  },
  // dockRepair procurement externalAssistance
  type: {
    type: String,
    default: ''
  }
})

const emit = defineEmits(['close', 'getList'])

const handleClose = () => {
  emit('close')
}

//
const winBidOptionsDisabled = (item) => {
  return formData.value.confirmQuotaBoList.some((p) => p.acceptCondition && item.dictValue === p.acceptCondition)
}

// 表单
const formEl = ref(null)
const formData = ref({
  confirmQuotaBoList: [{}]
})
const rules = reactive({
  acceptCondition: [{ required: true, message: t('userCenter.r4'), trigger: 'change' }],
  acceptConditionDescription: [{ required: true, message: t('userCenter.r5'), trigger: 'change' }]
})

// 取消
const cancel = () => {
  handleClose()
}

// 确定
const confirm = () => {
  if (!formEl.value) return
  formEl.value.validate(async (valid, fields) => {
    if (valid) {
      const params = formData.value.confirmQuotaBoList
      const obj = {
        dockRepair: {
          objTable: 'shcw_quotation_dock_order',
          apiFun: dockOrderConfirmQuotationApi
        },
        procurement: {
          objTable: 'shcw_quotation_procure_order',
          apiFun: procurementConfirmQuotationApi
        },
        externalAssistance: {
          objTable: 'shcw_quotation_assist_order',
          apiFun: externalAssistanceConfirmQuotationApi
        }
      }
      params.forEach((item) => {
        item.objId = props.objId
        item.objTable = obj[props.type].objTable
      })
      obj[props.type].apiFun(params).then(({ msg }) => {
        ElMessage.success(msg)
        handleClose()
        emit('getList')
      })
    }
  })
}

const init = () => {
  getDictData('win_bid_condition')
}
</script>

<style scoped lang="scss"></style>
